<?php
class FileAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('1', '2', '3'));
    }
    
    public function index() {
    
        $File = M('File');
        import("ORG.Util.Page");
        $count = $File->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page', $show);
        
        $file = $File->order(array('file_id' => 'desc'))->join(array('econ_category ON econ_file.cat_id = econ_category.cat_id'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('file', $file);
        $this->display();
    }
    
    public function add() {
    
        $Category = M('category');
        $category = $Category->where(array('cat_type_id' => 2))->order(array('cat_id' => 'asc'))->select();
        
        $this->assign('category', $category);
        $this->display();
    }
    
    public function upload() {
        if (isset($_POST['submitted'])) {
        
            $Config = A('Config');
            $Config->checkTeacherFile();
        
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();                                 // 实例化上传类
            $upload->savePath = './Public/Uploads/';                    // 设置附件上传目录
            $upload->saveRule = time;
            if (!$upload->upload()) {                                   // 上传错误 提示错误信息
                $this->error($upload->getErrorMsg());
            } else {                                                    // 上传成功 获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }
            
            // 保存表单数据 包括附件数据
            $File = D("File");                                          // 实例化File对象
            if ($File->create()) {                                      // 创建数据对象
                $File->file_savename = $info[0]["savename"];            // 保存上传的照片 根据需要自行组装
                $File->user_id = $_SESSION['user']['user_id'];
                if ($File->add()) {                                     // 写入用户数据到数据库
                    $this->success('上传文件成功！');
                } else {
                    $this->error('上传文件失败！');
                }
            } else {
                $this->error($File->getError());
            }
        }
    }
    
    public function download() {
        if (isset($_GET['savename'])) {
            $save_name = $_GET['savename'];
            $file = './Public/Uploads/' . $save_name; // 要下载的文件
            ob_clean();
            header('Pragma: public');
            header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
            header('Cache-Control:no-store, no-cache, must-revalidate');
            header('Cache-Control:pre-check=0, post-check=0, max-age=0');
            header('Content-Transfer-Encoding:binary'); 
            header('Content-Encoding:none');
            header('Content-type:multipart/form-data');
            header('Content-Disposition:attachment; filename="'.basename($file).'"'); //设置下载的默认文件名
            header('Content-length:'. filesize($file));
            $fp = fopen($file, 'r'); //读取数据，开始下载
            while(connection_status() == 0 && $buf = @fread($fp, 8192)){
                echo $buf;
            }
            fclose($fp);
            @flush();
            @ob_flush();
            exit();
        } else {
            $this->error('非法操作！');
        }
    }
    
    public function delete() {
        if (isset($_GET['id']) && isset($_GET['savename'])) {
        
            $Config = A('Config');
            $Config->checkTeacherFile();
            
            $id = (int) $_GET['id'];
            $save_name = $_GET['savename'];
            $File = M('File');
            
            if ($File->delete($id)) {
                if (file_exists('./Public/Uploads/' . $save_name)) {
                    unlink('./Public/Uploads/' . $save_name);
                    $this->success('成功删除文件！');
                } else {
                    $this->assign('waitSecond', 3);
                    $this->success('文件不存在！');
                }                
            } else {
                $this->error('删除文件失败！');
            }
        }
    }
    
    public function catIndex() {
        $Category = M('Category');
        import("ORG.Util.Page");
        $count = $Category->where(array('cat_type_id' => 2))->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page',$show);
        
        $category = $Category->where(array('cat_type_id' => 2))->order(array('cat_id' => 'asc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('category', $category);
        $this->display();
    }
    
    public function addCat() {
        $this->display();
    }
    
    public function editCat() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Category = M('Category');
            $category = $Category->find($id);
            
            $this->assign('category', $category);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
}
?>